import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
import * as menuService from '../../../services/menus';
const daoName = "BaseZbDeptNew";
const daoNameImport = "BaseZbDetpDtip";
const ButtonGroup = Button.Group;
const columns = [
    { title: '指标名称', width: 250, sorter: true, dataIndex: 'ZBMC', key: 'ZBMC', dispaly: true, filter: false, export: true },
    { title: '机构', width: 110, sorter: true, dataIndex: 'UTNM', key: 'UTNM', dispaly: true, filter: false, export: true },
    { title: '年份', width: 70, sorter: true, dataIndex: 'NF', key: 'NF', dispaly: true, filter: false, export: true },
    { title: '基期', width: 100, sorter: true, dataIndex: 'JQ', key: 'JQ', dispaly: true, filter: false, export: true },
    { title: '增量', width: 100, sorter: true, dataIndex: 'ZL', key: 'ZL', dispaly: true, filter: false, export: true },
    { title: '目标值', width: 100, sorter: true, dataIndex: 'MBZ', key: 'MBZ', dispaly: true, filter: false, export: true },
    {
        title: '一月',
        children: [
            { title: '增量', width: 100, sorter: true, dataIndex: 'ZL1', key: 'ZL1', dispaly: true, filter: true, export: true },
            { title: '目标', width: 100, sorter: true, dataIndex: 'MB1', key: 'MB1', dispaly: true, filter: true, export: true }
        ],
        dispaly: true,
        dataIndex: '一月'
    }, {
        title: '二月',
        children: [
            { title: '增量', width: 100, sorter: true, dataIndex: 'ZL2', key: 'ZL2', dispaly: true, filter: true, export: true },
            { title: '目标', width: 100, sorter: true, dataIndex: 'MB2', key: 'MB2', dispaly: true, filter: true, export: true }
        ],
        dispaly: true,
        dataIndex: '二月'
    }, {
        title: '三月',
        children: [
            { title: '增量', width: 100, sorter: true, dataIndex: 'ZL3', key: 'ZL3', dispaly: true, filter: true, export: true },
            { title: '目标', width: 100, sorter: true, dataIndex: 'MB3', key: 'MB3', dispaly: true, filter: true, export: true }
        ],
        dispaly: true,
        dataIndex: '三月'
    }, {
        title: '四月',
        children: [
            { title: '增量', width: 100, sorter: true, dataIndex: 'ZL4', key: 'ZL4', dispaly: true, filter: true, export: true },
            { title: '目标', width: 100, sorter: true, dataIndex: 'MB4', key: 'MB4', dispaly: true, filter: true, export: true }
        ],
        dispaly: true,
        dataIndex: '四月'
    }, {
        title: '五月',
        children: [
            { title: '增量', width: 100, sorter: true, dataIndex: 'ZL5', key: 'ZL5', dispaly: true, filter: true, export: true },
            { title: '目标', width: 100, sorter: true, dataIndex: 'MB5', key: 'MB5', dispaly: true, filter: true, export: true }
        ],
        dispaly: true,
        dataIndex: '五月'
    }, {
        title: '六月',
        children: [
            { title: '增量', width: 100, sorter: true, dataIndex: 'ZL6', key: 'ZL6', dispaly: true, filter: true, export: true },
            { title: '目标', width: 100, sorter: true, dataIndex: 'MB6', key: 'MB6', dispaly: true, filter: true, export: true }
        ],
        dispaly: true,
        dataIndex: '六月'
    }, {
        title: '七月',
        children: [
            { title: '增量', width: 100, sorter: true, dataIndex: 'ZL7', key: 'ZL7', dispaly: true, filter: true, export: true },
            { title: '目标', width: 100, sorter: true, dataIndex: 'MB7', key: 'MB7', dispaly: true, filter: true, export: true }
        ],
        dispaly: true,
        dataIndex: '七月'
    }, {
        title: '八月',
        children: [
            { title: '增量', width: 100, sorter: true, dataIndex: 'ZL8', key: 'ZL8', dispaly: true, filter: true, export: true },
            { title: '目标', width: 100, sorter: true, dataIndex: 'MB8', key: 'MB8', dispaly: true, filter: true, export: true }
        ],
        dispaly: true,
        dataIndex: '八月'
    }, {
        title: '九月',
        children: [
            { title: '增量', width: 100, sorter: true, dataIndex: 'ZL9', key: 'ZL9', dispaly: true, filter: true, export: true },
            { title: '目标', width: 100, sorter: true, dataIndex: 'MB9', key: 'MB9', dispaly: true, filter: true, export: true }
        ],
        dispaly: true,
        dataIndex: '九月'
    }, {
        title: '十月',
        children: [
            { title: '增量', width: 100, sorter: true, dataIndex: 'ZL10', key: 'ZL10', dispaly: true, filter: true, export: true },
            { title: '目标', width: 100, sorter: true, dataIndex: 'MB10', key: 'MB10', dispaly: true, filter: true, export: true }
        ],
        dispaly: true,
        dataIndex: '十月'
    }, {
        title: '十一月',
        children: [
            { title: '增量', width: 100, sorter: true, dataIndex: 'ZL11', key: 'ZL11', dispaly: true, filter: true, export: true },
            { title: '目标', width: 100, sorter: true, dataIndex: 'MB11', key: 'MB11', dispaly: true, filter: true, export: true }
        ],
        dispaly: true,
        dataIndex: '十一月'
    }, {
        title: '十二月',
        children: [
            { title: '增量', width: 100, sorter: true, dataIndex: 'ZL12', key: 'ZL12', dispaly: true, filter: true, export: true },
            { title: '目标', width: 100, sorter: true, dataIndex: 'MB12', key: 'MB12', dispaly: true, filter: true, export: true }
        ],
        dispaly: true,
        dataIndex: '十二月'
    }
];

const controls = [
    { name: 'ZBMC', type: 'text', label: '指标名称', grid: 12 / 24 },
    { name: 'UTNM', type: 'text', label: '机构', grid: 12 / 24 },
    { name: 'NF', type: 'text', label: '年份', grid: 12 / 24 },
    { name: 'JQ', type: "number", dataType: "number", label: '基期', grid: 12 / 24 },
    { name: 'ZL', type: "number", dataType: "number", label: '增量', grid: 12 / 24 },
    { name: 'MBZ', type: "number", dataType: "number", label: '目标值', grid: 12 / 24 },
    { name: 'ZL1', type: "number", dataType: "number", label: '一月增量', grid: 12 / 24 },
    { name: 'MB1', type: "number", dataType: "number", label: '一月目标', grid: 12 / 24 },
    { name: 'ZL2', type: "number", dataType: "number", label: '二月增量', grid: 12 / 24 },
    { name: 'MB2', type: "number", dataType: "number", label: '二月目标', grid: 12 / 24 },
    { name: 'ZL3', type: "number", dataType: "number", label: '三月增量', grid: 12 / 24 },
    { name: 'MB3', type: "number", dataType: "number", label: '三月目标', grid: 12 / 24 },
    { name: 'ZL4', type: "number", dataType: "number", label: '四月增量', grid: 12 / 24 },
    { name: 'MB4', type: "number", dataType: "number", label: '四月目标', grid: 12 / 24 },
    { name: 'ZL5', type: "number", dataType: "number", label: '五月增量', grid: 12 / 24 },
    { name: 'MB5', type: "number", dataType: "number", label: '五月目标', grid: 12 / 24 },
    { name: 'ZL6', type: "number", dataType: "number", label: '六月增量', grid: 12 / 24 },
    { name: 'MB6', type: "number", dataType: "number", label: '六月目标', grid: 12 / 24 },
    { name: 'ZL7', type: "number", dataType: "number", label: '七月增量', grid: 12 / 24 },
    { name: 'MB7', type: "number", dataType: "number", label: '七月目标', grid: 12 / 24 },
    { name: 'ZL8', type: "number", dataType: "number", label: '八月增量', grid: 12 / 24 },
    { name: 'MB8', type: "number", dataType: "number", label: '八月目标', grid: 12 / 24 },
    { name: 'ZL9', type: "number", dataType: "number", label: '九月增量', grid: 12 / 24 },
    { name: 'MB9', type: "number", dataType: "number", label: '九月目标', grid: 12 / 24 },
    { name: 'ZL10', type: "number", dataType: "number", label: '十月增量', grid: 12 / 24 },
    { name: 'MB10', type: "number", dataType: "number", label: '十月目标', grid: 12 / 24 },
    { name: 'ZL11', type: "number", dataType: "number", label: '十一月增量', grid: 12 / 24 },
    { name: 'MB11', type: "number", dataType: "number", label: '十一月目标', grid: 12 / 24 },
    { name: 'ZL12', type: "number", dataType: "number", label: '十二月增量', grid: 12 / 24 },
    { name: 'MB12', type: "number", dataType: "number", label: '十二月目标', grid: 12 / 24 },
];
const controlsDtip = [
    { name: 'ZBMC', type: 'text', label: '指标名称', grid: 12 / 24 },
    { name: 'UTNM', type: 'text', label: '机构', grid: 12 / 24 },
    { name: 'NF', type: 'text', label: '年份', grid: 12 / 24,placeholder:"按YYYY格式输入,例如:2019",min: 4, max: 4 },
    { name: 'JQ', type: "number", dataType: "number", label: '基期', grid: 12 / 24 },
    { name: 'ZL', type: "number", dataType: "number", label: '增量', grid: 12 / 24 },
    { name: 'MBZ', type: "number", dataType: "number", label: '目标值', grid: 12 / 24 },
    { name: 'ZL1', type: "number", dataType: "number", label: '一月增量', grid: 12 / 24 },
    { name: 'MB1', type: "number", dataType: "number", label: '一月目标', grid: 12 / 24 },
    { name: 'ZL2', type: "number", dataType: "number", label: '二月增量', grid: 12 / 24 },
    { name: 'MB2', type: "number", dataType: "number", label: '二月目标', grid: 12 / 24 },
    { name: 'ZL3', type: "number", dataType: "number", label: '三月增量', grid: 12 / 24 },
    { name: 'MB3', type: "number", dataType: "number", label: '三月目标', grid: 12 / 24 },
    { name: 'ZL4', type: "number", dataType: "number", label: '四月增量', grid: 12 / 24 },
    { name: 'MB4', type: "number", dataType: "number", label: '四月目标', grid: 12 / 24 },
    { name: 'ZL5', type: "number", dataType: "number", label: '五月增量', grid: 12 / 24 },
    { name: 'MB5', type: "number", dataType: "number", label: '五月目标', grid: 12 / 24 },
    { name: 'ZL6', type: "number", dataType: "number", label: '六月增量', grid: 12 / 24 },
    { name: 'MB6', type: "number", dataType: "number", label: '六月目标', grid: 12 / 24 },
    { name: 'ZL7', type: "number", dataType: "number", label: '七月增量', grid: 12 / 24 },
    { name: 'MB7', type: "number", dataType: "number", label: '七月目标', grid: 12 / 24 },
    { name: 'ZL8', type: "number", dataType: "number", label: '八月增量', grid: 12 / 24 },
    { name: 'MB8', type: "number", dataType: "number", label: '八月目标', grid: 12 / 24 },
    { name: 'ZL9', type: "number", dataType: "number", label: '九月增量', grid: 12 / 24 },
    { name: 'MB9', type: "number", dataType: "number", label: '九月目标', grid: 12 / 24 },
    { name: 'ZL10', type: "number", dataType: "number", label: '十月增量', grid: 12 / 24 },
    { name: 'MB10', type: "number", dataType: "number", label: '十月目标', grid: 12 / 24 },
    { name: 'ZL11', type: "number", dataType: "number", label: '十一月增量', grid: 12 / 24 },
    { name: 'MB11', type: "number", dataType: "number", label: '十一月目标', grid: 12 / 24 },
    { name: 'ZL12', type: "number", dataType: "number", label: '十二月增量', grid: 12 / 24 },
    { name: 'MB12', type: "number", dataType: "number", label: '十二月目标', grid: 12 / 24 },
];
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
const DataImport = dataImportCreate(daoNameImport);
class ZHJJZBJHDRForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataChange: false
        };
    }
    componentDidMount() {
        this.props.dispatch({
            type: `${daoName}/setKeyName`,
            payload: { keyName: "ID", permID: this.props.permID }
        });

        let cmDate = datetime.format(this.props.cmDate, "yyyy-MM-dd").toString()
        let whereItems = [];
        let WhereCauses = [];
        whereItems.push({
            FieldName: "NF",
            Operator: "=",
            FieldValues: cmDate.substring(0, 4)
        }, {
                FieldName: "UTID",
                Operator: "=",
                FieldValues: '15'
            })
        WhereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        let seeks = WhereCauses
        let sort = {
            field: 'ORDER',
            order: 'ASC'
        }
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 20, seeks, sorter: sort }
        });
        this.loadOptionData();
    }
    componentWillReceiveProps(nextProps) {
        const { showImport } = this.props
        if (!nextProps.showImport && showImport) {
            this.props.dispatch({
                type: `${daoName}/fetch`,
                payload: { daoName, page: 1, pageSize: 20 }
            });
        }
    }
    loadOptionData() {
        let options = []
        controls.map(c => {
            if (c.fetch) {
                let f = c.fetch;
                if (c.type === 'cascader-select') {
                    f.isCascader = true;
                }
                if (c.type !== 'search') {
                    options.push(f);
                }
            }
        })
        if (daoName && options.length > 0) {
            this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
        }
    }
    showImportHandler() {
        this.props.dispatch({
            type: `${daoNameImport}_DataImport/setDisplay`,
            payload: { display: true },
        });
    }
    DownTmportHandler() {
        let columnHeaders = []
        let header = {
            Name: 'ZBMC',
            Caption: '指标名称',
            Width: 350
        }
        let header1 = {
            Name: 'UTNM',
            Caption: '机构',
            Width: 100
        }
        let header2 = {
            Name: 'JQ',
            Caption: '基期',
            Width: 100
        }
        let header3 = {
            Name: 'ZL',
            Caption: '增量',
            Width: 100
        }
        let header4 = {
            Name: 'MBZ',
            Caption: '目标值',
            Width: 100
        }
        let header5 = {
            Name: 'ZL1',
            Caption: '一月增量',
            Width: 100
        }
        let header6 = {
            Name: 'MB1',
            Caption: '一月目标',
            Width: 100
        }
        let header7 = {
            Name: 'ZL2',
            Caption: '二月增量',
            Width: 100
        }
        let header8 = {
            Name: 'MB2',
            Caption: '二月目标',
            Width: 100
        }
        let header9 = {
            Name: 'ZL3',
            Caption: '三月增量',
            Width: 100
        }
        let header10 = {
            Name: 'MB3',
            Caption: '三月目标',
            Width: 100
        }
        let header11 = {
            Name: 'ZL4',
            Caption: '四月增量',
            Width: 100
        }
        let header12 = {
            Name: 'MB4',
            Caption: '四月目标',
            Width: 100
        }
        let header13 = {
            Name: 'ZL5',
            Caption: '五月增量',
            Width: 100
        }
        let header14 = {
            Name: 'MB5',
            Caption: '五月目标',
            Width: 100
        }
        let header15 = {
            Name: 'ZL6',
            Caption: '六月增量',
            Width: 100
        }
        let header16 = {
            Name: 'MB6',
            Caption: '六月目标',
            Width: 100
        }
        let header17 = {
            Name: 'ZL7',
            Caption: '七月增量',
            Width: 100
        }
        let header18 = {
            Name: 'MB7',
            Caption: '七月目标',
            Width: 100
        }
        let header19 = {
            Name: 'ZL8',
            Caption: '八月增量',
            Width: 100
        }
        let header20 = {
            Name: 'MB8',
            Caption: '八月目标',
            Width: 100
        }
        let header21 = {
            Name: 'ZL9',
            Caption: '九月增量',
            Width: 100
        }
        let header22 = {
            Name: 'MB9',
            Caption: '九月目标',
            Width: 100
        }
        let header23 = {
            Name: 'ZL10',
            Caption: '十月增量',
            Width: 100
        }
        let header24 = {
            Name: 'MB10',
            Caption: '十月目标',
            Width: 100
        }
        let header25 = {
            Name: 'ZL11',
            Caption: '十一月增量',
            Width: 100
        }
        let header26 = {
            Name: 'MB11',
            Caption: '十一月目标',
            Width: 100
        }
        let header27 = {
            Name: 'ZL12',
            Caption: '十二月增量',
            Width: 100
        }
        let header28 = {
            Name: 'MB12',
            Caption: '十二月目标',
            Width: 100
        }
        columnHeaders.push(header)
        columnHeaders.push(header1)
        columnHeaders.push(header2)
        columnHeaders.push(header3)
        columnHeaders.push(header4)
        columnHeaders.push(header5)
        columnHeaders.push(header6)
        columnHeaders.push(header7)
        columnHeaders.push(header8)
        columnHeaders.push(header9)
        columnHeaders.push(header10)
        columnHeaders.push(header11)
        columnHeaders.push(header12)
        columnHeaders.push(header13)
        columnHeaders.push(header14)
        columnHeaders.push(header15)
        columnHeaders.push(header16)
        columnHeaders.push(header17)
        columnHeaders.push(header18)
        columnHeaders.push(header19)
        columnHeaders.push(header20)
        columnHeaders.push(header21)
        columnHeaders.push(header22)
        columnHeaders.push(header23)
        columnHeaders.push(header24)
        columnHeaders.push(header25)
        columnHeaders.push(header26)
        columnHeaders.push(header27)
        columnHeaders.push(header28)
        let form = document.createElement('form');//定义一个form表单
        let whereItems = [];
        whereItems.push({
            FieldName: "NF",
            Operator: "=",
            FieldValues: datetime.format(this.props.cmDate, "yyyy-MM-dd").toString().substring(0, 4)
        })
        const whereCauses = []
        whereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        const orderItems = [{
            FieldName: 'UTID',
            Direction: 'DESC'
        }, {
            FieldName: 'ORDER',
            Direction: 'ASC'
        }]
        form.style = "display:none";
        form.target = "_blank";
        form.method = "post";
        form.action = "/api/ExportXlsx/" + daoName;
        let input1 = document.createElement("input");
        input1.type = "hidden";
        input1.name = "WhereCauses";
        input1.value = JSON.stringify(whereCauses);
        let input2 = document.createElement("input");
        input2.type = "hidden";
        input2.name = "OrderItems";
        input2.value = JSON.stringify(orderItems)
        let input3 = document.createElement("input");
        input3.type = "hidden";
        input3.name = "ColumnHeaders";
        input3.value = JSON.stringify(columnHeaders);
        let input4 = document.createElement("input");
        input4.type = "hidden";
        input4.name = "PermID";
        input4.value = "" + this.props.permID;
        let input5 = document.createElement("input");
        input5.type = "hidden";
        input5.name = "UserID";
        input5.value = "" + this.props.userID;
        document.body.appendChild(form);//将表单放置在web中
        form.appendChild(input1);
        form.appendChild(input2);
        form.appendChild(input3);
        form.appendChild(input4);
        form.appendChild(input5);
        form.submit();
        document.body.removeChild(form)
    }
    searchHandle(data) {
        this.state.searchData = data
    }
    findData() {
        let cmdate = datetime.format(this.props.cmDate, "yyyy-MM-dd").toString().substring(0, 4)
        const form = this.refs.findForm.getForm();
        let isOK = true;
        let nf = ""
        let jg = ""
        form.validateFields((err, values) => {
            if (!err) {
                if (values.NF === undefined || values.ZB === '') {
                    nf = cmdate
                } else {
                    nf = values.NF
                }
                if (values.UTID === undefined || values.UTID === '') {
                    message.error('机构不能为空！');
                    isOK = false;
                }
                jg = values.UTID
            }
        });
        if (!isOK) {
            return;
        }
        let whereItems = [];
        let WhereCauses = [];
        whereItems.push({
            FieldName: "NF",
            Operator: "=",
            FieldValues: nf
        }, {
                FieldName: "UTID",
                Operator: "=",
                FieldValues: jg
            })
        WhereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        let seeks = WhereCauses
        let sort = {
            field: 'ORDER',
            order: 'ASC'
        }
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 20, seeks, sorter: sort }
        });
    }
    render() {
        let { showEdit, record, showImport, permID, popedomTypes, cmDate } = this.props
        const pTypes = popedomTypes[permID]
        const controlsFilter = [
            {
                name: "NF", label: "年份", type: "default", grid: 3 / 24, required: false,
                layout: { labelSpan: 10, wrapperSpan: 14 }
                
            }, {
                name: "UTID", label: "机构", type: "search", grid: 4 / 24, required: false,
                dataType: "number", fetch: { daoName: "Organization", searchColumns: ['No', 'Name'], optionTpl: 'Name', valueTpl: "ID", onSelect: this.searchHandle.bind(this) },
                layout: { labelSpan: 7, wrapperSpan: 15 }, placeholder: "请输入机构"
            }, {
                name: 'XS', required: false, type: 'button-group',
                data: [
                    { value: '1', label: '查询', onClick: this.findData.bind(this) },
                    { value: '2', label: '导入', onClick: this.showImportHandler.bind(this) },
                    { value: '3', label: '下载导入模板', onClick: this.DownTmportHandler.bind(this) }
                ],
                grid: 10 / 24, layout: { labelSpan: 7, wrapperSpan: 15 }
            },
        ];
        return (
            <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
                <div>
                    <div className={styles.create}>
                        <EditFrom ref="findForm" daoName={daoName} controls={controlsFilter} clearItemMargin={true} selectFirst={false} record={{ NF: "2019", UTID: "15" }} />
                    </div>
                    {/* <font style={{ color: "red" }}>注：默认展示今年【{datetime.format(this.props.cmDate, "yyyy-MM-dd").toString().substring(0, 4)}】营业部数据</font> */}
                    <DataTable ref="BaseZbDeptDtip" daoName={daoName} columns={columns} controls={controls} permID={permID} canExport={false} />
                    <DataImport title="综合经营指标计划" importName="BaseZbDeptDtip" daoName={daoNameImport} keys={["ID"]} paramNames={["NF"]} importMode="DeleteAfterInsert" controls={controlsDtip} />
                </div>
            </div>
        )
    }
}

function mapStateToProps(state) {
    const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
    const { userID, orgID, pOrgID, popedomTypes, cmDate } = state.menus;
    const { display } = state[`${daoNameImport}_DataImport`];
    return {
        userID,
        orgID,
        pOrgID,
        popedomTypes,
        showEdit,
        selectedRowKeys,
        keyName,
        record,
        showImport: display,
        cmDate
    }
}
ZHJJZBJHDRForm = connect(mapStateToProps)(ZHJJZBJHDRForm)
export default ZHJJZBJHDRForm;

let render = (props) => React.createElement(ZHJJZBJHDRForm, props);
let f = new Factory()
f.set("ZHJJZBJHDRForm", render)
